package com.aptemo.webetek.client.widgets.paging;

import java.util.HashMap;
import java.util.Map;

import com.aptemo.webetek.client.mvp.BasePresenter;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;

public abstract class PagingPresenter implements BasePresenter
{

	private PagingDisplay			mdisplay;

	private int						pageSize	= 5;
	private int						totalCnt	= 0;
	private Map<Integer, String>	pages		= new HashMap<Integer, String>();

	public PagingPresenter(PagingDisplay display)
	{
		this(display, 5);
	}

	public PagingPresenter(PagingDisplay display, int pageSize)
	{
		this.mdisplay = display;
		this.pageSize = pageSize;
	}
	
	public Widget getWidget()
	{
		return mdisplay.getDisplay();
	}

	@Override
	public void go(HasWidgets container)
	{
	}

	protected String getCursor(int page)
	{
		return pages.get(page);
	}

	protected int getPageSize()
	{
		return pageSize;
	}

	protected void setPagingTotalSize(int size)
	{
		this.totalCnt = size;
	}

	public int updatePaging(int page, String cursor)
	{
		int pageNo = -1;
		// check if page no was correct
		if (pages.containsKey(page))
		{
			pageNo = page;
		}
		else
		{
			pageNo = 0;
		}
		if (page == pageNo)
		{
			pages.put(pageNo + 1, cursor);
		}
		else
		{
			// TODO: r u sure?
			resetPaging();
		}
		mdisplay.update(pageNo * pageSize, pageSize, totalCnt);

		return pageNo;
	}

	protected void resetPaging()
	{
		pages.clear();
	}

}
